//var basePath ="[[@{/}]]";//项目根路径
console.log(basePath);

function register(){
    var email = $("#email").val();
    var password = $("#password").val();
    var passwordtwo = $("#passwordtwo").val();
    if(email === "" || email == null){
        layer.msg("邮箱不能为空");
        return false;
    }
    if(password === "" || password == null){
        layer.msg("密码不能为空");
        return false;
    }

    if(password.length < 6 || password.length > 18){
        layer.msg("密码长度不符合要求");
        return false;
    }
    var regex = /^\w{5,}@[a-z0-9]{2,3}\.[a-z]+$|\,$/;
    if(!regex.test(email)){
        layer.msg("邮箱账号不规范");
        return false;
    }

    if(password !== passwordtwo){
        layer.msg("密码不一致");
        return false;
    }

    //判断邮箱验证码
    var identify = $("#code").val();
    var object = {
        email : email,
        identify:identify
    };
    $.post(basePath+"reg/sendIdentify",object,function (hashMap) {
        console.log("验证码:   "+hashMap.success);
        if(!hashMap.success){
            layer.msg(hashMap.message);
            return;
        }
        var nick = email.split("@")[0];
        password = MD5(password);
        var obj = {
            email : email,
            password : password,
            nick:nick
        };

        $.post(basePath+"reg/register",obj,function (map) {
            layer.msg(map.message);
            if(map.success){
                layer.msg("进入登录页面");
                window.location.href=basePath+"logic";

            }
        })

    });

}

function emailIdentify(time){
    if(time > 1){
        $(".send-btn").css("background-color","#ccc");
        time--;
        $(".send-btn").val(time+"s后重新发送");
        $(".send-btn").attr("disabled",true);
        setTimeout(function () {
            emailIdentify(time);
        },1000);

    }else{
        $(".send-btn").css("background-color","#008B8B");
        $(".send-btn").val("获取验证码");
        $(".send-btn").attr("disabled",false);
    }
};

$(function () {
    //注册
    $("#register").click(register);

    //获取邮箱验证码
    $(".send-btn").click(function () {

        var email = $("#email").val();
        var password = $("#password").val();
        if(email === "" || email == null){
            layer.msg("邮箱不能为空");
            return false;
        }

        var regex = /^\w{5,}@[a-z0-9]{2,3}\.[a-z]+$|\,$/;
        if(!regex.test(email)){
            layer.msg("邮箱账号不规范");
            return false;
        }

        //获取图形验证码
        var identify = $("#imgcode").val();
        var object = {
            identify:identify
        };
        var o = {
            email: email
        };
        console.log("ident:   "+identify);
        $.post(basePath+"user/identify",object,function (data) {//校验图形验证码是否正确，正确才能获取邮箱验证码
            console.log("图形验证码***********" + data);
            if (!data) {
                layer.msg("图形验证码错误");

            }else{
                $.post(basePath+"send/send",o,function (map) {
                    layer.msg(map.message);
                    if(map.success){
                        emailIdentify(90);
                    }
                })
            }
        });

    });
    //获取验证码的点击事件
    $("#codeImg").click(function () {
        $("#codeImg").attr("src",basePath+"verifyCode/verify?time="+new Date().getTime());
    });

})